package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    //查询当前线索的所有跟进记录
    @Override
    public List<TbClueTrackRecord> selectAllClueTrackRecord(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.selectAllClueTrackRecord(clueId);
        return list;
    }

    //添加线索的跟进记录
    @Transactional
    @Override
    public void addClueTrackRecord(ClueTrackRecordVo tbClueTrackRecord) {
        //1、首先得到clue的数据，进行修改
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(tbClueTrackRecord,tbClue,"id");
        //重新对clueId进行赋值
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClueMapper.updateTbClue(tbClue);

        //2、对trackRecord进行复制，以及添加
        TbClueTrackRecord trackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(tbClueTrackRecord,trackRecord);
        String username = SecurityUtils.getUsername();
        trackRecord.setCreateBy(username);
        tbClueTrackRecordMapper.addTrackRecord(trackRecord);
    }
}
